﻿$(function() {
	/**
	 * 设备开动情况
	 * */
    var dayline = echarts.init(document.getElementById('singlechart'));
    optionline = {
            tooltip : {
                trigger: 'axis',
                axisPointer: {
    		        type: 'shadow'
    		    }
            },
            backgroundColor:'#e0e0e0',
            grid: {
                top: '15%',
                left: '4%',
                right: '5%',
                bottom:'20%',
                containLabel: true
            },
            legend: {
            	bottom: '20',
    	        data:['平均开动率','平均满载率']
    	    },
            xAxis:  {
                type: 'category',
                boundaryGap: [1, 1],
                data: [],//['10','11','12','13','14','15'],
//                axisLabel:{
//    	    		formatter: '{value}周'
//    	    	 },
                name:'小时',
                splitLine:{
                	show:false
                }
            },
            yAxis: {
            	min:0,
            	max:100,
                type: 'value',
                name:'开动率(%)',
                axisLabel:{
    	    		 formatter: '{value}'
    	    	 },
                splitLine:{
                	show:false
                }
            },
//            dataZoom:{
//    	    	type:'slider',
//    	    	start:0,
//    	    	end:50,
////    	    	labelFormatter: function (value) {
////    	    	    return value; // 返回最终的label内容。
////    	    	},
//    	    	bottom:20
//    	    },
            series: [{
                 name:'平均开动率',
                 type:'line',
                 data:[]//[0.8,0.9,0.84,0.7,0.8,0.91],
             },{
                 name:'平均满载率',
                 type:'line',
                 data:[]//[0.83,0.75,0.89,0.91,0.79,0.93],
             }]
        };
    dayline.setOption(optionline);
    
    
    var lineId=0;var equId=0;
    var	startWeek="";
    var	endWeek="";
    var year=moment().year();
    
    /**
     * slider滑动事件
     * */
    $('#ss').slider({
    	range: true,
        onComplete:function(value){//滑块值改变时触发
	       	startWeek=value[0];
	       	endWeek=value[1];
	       	getData();
       }
    });
    
    /**
     * 初始化
     * */
    var vm = new Vue({
	    el: '#itemData',
	    data:{item:[]}
	}); 
    var vm2 = new Vue({
	    el: '#itemData2',
	    data:{item:[]}
	}); 
    var currentWeek=moment().week();
    var weeks="";
    if(currentWeek<8){
    	weeks=[1,currentWeek];
    }else{
    	weeks=[moment().subtract(7, 'w').week(),currentWeek];
    }
    $('#ss').slider('setValues',weeks);
    var arr=$('#ss').slider('getValues');
    startWeek=arr[0];
    endWeek=arr[1];
    getData();
    function getData(){
    	$.post(basePath+"/line/StartrateWeek.json",{"lineId":lineId,"equId":equId,year:year,startTime:startWeek,endTime:endWeek},function(res){
    		vm.$set('item',res);
    		vm2.$set('item',res);
//		if(res.success){
			dayline.setOption({
		        xAxis: {
		            data: res.startRateWeekss
		        },
		        series: [{
		        	name:'平均开动率',
		            data: res.startRate
		        },{
		        	name:'平均满载率',
		            data: res.fullRate
		        }]
			});
//		}else{
//			alert("日期无效");
//		}	
	});
    }
  function switchEquipment(){
	$.post(basePath+"/line/switchEquipment/week.json",{"equId":equId,year:year,startTime:startWeek,endTime:endWeek},function(res){
		vm2.$set('item',res);
		dayline.setOption({
	        xAxis: {
	            data: res.startRateWeekss
	        },
	        series: [{
	        	name:'平均开动率',
	            data: res.startRate
	        },{
	        	name:'平均满载率',
	            data: res.fullRate
	        }]
		});
	});
} 
  //点击事件
	 $("#defineTime").click(function (){
		 var mePanel = $('#defineTimeWin');
	   	 mePanel.dialog('open');
	 });
	//定义时间的表单
	$('#defineTimeWin').dialog({
		title:'自定义时间',
		width:400,
		height:150,
		iconCls:'icon-add',
		closed: true,
		modal: true,
		inline:true,
		buttons:[{
			text:'确定',
			iconCls:'icon-add-new',
			handler:function(){
					var st=$("input[name='startTime']").val();
					var cmax=moment(st, "YYYY").weeksInYear();
					year=st;
					$('#ss').slider({
						max:cmax
					});
					getData();
					$('#defineTimeWin').dialog('close');

			}
		},{
			text:'取消',
			iconCls:'icon-redo',
			handler:function(){
				$('#defineTimeWin').dialog('close');
			}
		}],
		onClose: function() {
			$('#defineTimeform').form('reset');
		}
	});
	/**
	  * 切换生产线的点击事件
	  * */
	var mePanel = $('#switchEquip');
	$("#linetree").tree({
		onDblClick: function(node) {
			if (node.url) {
//				if(node.shifts){
					lineId=node.id;
//					var arr=$('#ss').slider('getValues');
//		 		    var time1=arr[0];
//		 		    var time2=arr[1];
//		 		    startTime=moment(time1).format("YYYY-MM-DD");
//		 		    endTime=moment(time2).format("YYYY-MM-DD");
		 		    getData();
		 		    mePanel.dialog('close');
//				}else{
//					$.messager.alert({showSpeed:100,title:'消息提示',msg:'该生产线无班次，请重新选择',timeout:3000});
//				}
			}else{
				$.messager.alert({showSpeed:100,title:'消息提示',msg:'请选择生产线！',timeout:3000});
			}
		}
	});
  $("#switchEquipOK").click(function(){
  	var row = $("#linetree").tree('getSelected');
  	if(row&&row.url!=null){
		lineId=row.id;
//		var arr=$('#ss').slider('getValues');
//	    var time1=arr[0];
//	    var time2=arr[1];
//	    startTime=moment(time1).format("YYYY-MM-DD");
//	    endTime=moment(time2).format("YYYY-MM-DD");
	    getData();
	    $('#switchEquip').dialog('close');
  	}else{
		$.messager.alert({showSpeed:100,title:'消息提示',msg:'请选择生产线！',timeout:3000});
	}
  });
  
  
  /**
   * 选择设备
   */
  $("#switchEquipbtn").click(function(){
		var equPanel = $('#allEquipsByLine');
		equPanel.dialog('open');
		$('#equip_data').datagrid({    
		    url: basePath+"/equip/findByLine.json",
		    queryParams:{lineId:lineId}
		});
  });
  $('#equip_data').datagrid({
		//双击设备时，根据选中设备刷新
		onDblClickRow: function(rowIndex, rowData){
			equId=rowData.id;
			switchEquipment();
		   $("#allEquipsByLine").dialog('close');
		}
	});
  $("#switchEquipByLineOK").click(function(){
	   	var equWin=$("#allEquipsByLine");
	   	var row=$('#equip_data').datagrid('getSelected');
	   	equId=row.id;
	   	switchEquipment();
	   	equWin.dialog('close');
  });
});